﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaSetAIBattleDialog : Form
    {
        PListDict curDict;
        public EditDramaSetAIBattleDialog()
        {
            InitializeComponent();
            foreach (Hero hero in UCDrama.heros)
            {
                comboBoxHero.Items.Add(hero.name);
                comboBoxTargetHero.Items.Add(hero.name);
            }
            foreach (KeyValuePair<int, string> kv in Constant.aiType)
            {
                comboBoxAIType.Items.Add(kv.Value);
            }
        }
        public void Show(IWin32Window owner, PListDict dict)
        {
            listBoxHeros.Items.Clear();
            curDict = (PListDict)dict["DataDic"];
            foreach (KeyValuePair<string, IPListElement> kv in curDict)
            {
                PListDict unitDict = (PListDict)kv.Value;
                PListInteger unitNum = (PListInteger)unitDict["UnitNum"];
                Hero hero = UCDrama.heros.Find((h) => { return h.num == unitNum.Value; });
                listBoxHeros.Items.Add(hero.name);
            }
            if (listBoxHeros.Items.Count > 0)
                listBoxHeros.SelectedIndex = 0;
            if (listBoxHeros.Items.Count <= 0)
                buttonDel.Enabled = false;
            else
                buttonDel.Enabled = true;
            this.ShowDialog(owner);
        }

        private void listBoxHeros_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxHeros.SelectedItem == null)
                return;
            Hero hero = UCDrama.heros.Find((h) => { return h.name == listBoxHeros.SelectedItem.ToString(); });
            comboBoxHero.SelectedItem = hero.name;
            foreach (KeyValuePair<string, IPListElement> kv in curDict)
            {
                PListDict unitDict = (PListDict)kv.Value;
                PListInteger unitNum = (PListInteger)unitDict["UnitNum"];
                if (unitNum.Value == hero.num)
                {
                    PListInteger coordX = (PListInteger)unitDict["CoordX"];
                    PListInteger coordY = (PListInteger)unitDict["CoordY"];
                    PListInteger direction = (PListInteger)unitDict["Direction"];
                    PListInteger levelAdd = (PListInteger)unitDict["LevelAdd"];
                    PListInteger teamLimit = (PListInteger)unitDict["TeamLimit"];
                    PListBool isHidden = (PListBool)unitDict["IsHidden"];
                    PListBool isEnemyHelper = (PListBool)unitDict["IsEnemyHelper"];
                    PListDict aiDict = (PListDict)unitDict["AIType"];
                    PListInteger aiType = (PListInteger)aiDict["Type"];
                    PListDict targetDict = (PListDict)aiDict["Target"];
                    PListInteger targetX = (PListInteger)targetDict["CoordX"];
                    PListInteger targetY = (PListInteger)targetDict["CoordY"];
                    PListInteger targetUnit = (PListInteger)targetDict["UnitID"];
                    textBoxX.Text = coordX.Value.ToString();
                    textBoxY.Text = coordY.Value.ToString();
                    comboBoxDirection.SelectedIndex = (int)direction.Value;
                    numericUpDownLv.Value = levelAdd.Value;
                    comboBoxTeamLimit.SelectedIndex = (int)teamLimit.Value;
                    comboBoxAIType.SelectedIndex = (int)aiType.Value;
                    textBoxTargetX.Text = targetX.Value.ToString();
                    textBoxTargetY.Text = targetY.Value.ToString();
                    Hero targetHero = UCDrama.heros.Find((h) => { return h.num == targetUnit.Value; });
                    comboBoxTargetHero.SelectedItem = targetHero.name;
                    checkBoxIsHidden.Checked = isHidden.Value;
                    checkBoxIsEnemyHelper.Checked = isEnemyHelper.Value;
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            
            Hero hero = UCDrama.heros.Find((h) => { return h.name == comboBoxHero.Text; });
            if (listBoxHeros.SelectedItem!=null&&hero.name == listBoxHeros.SelectedItem.ToString())
            {
                MessageBox.Show("该武将已被添加");
                return;
            }
            PListDict unitDict = new PListDict();
            curDict.Add("Unit" + hero.num, unitDict);
            unitDict.Add("UnitNum", new PListInteger(hero.num));
            unitDict.Add("CoordX", new PListInteger(System.Int32.Parse(textBoxX.Text)));
            unitDict.Add("CoordY", new PListInteger(System.Int32.Parse(textBoxY.Text)));
            unitDict.Add("Direction", new PListInteger(comboBoxDirection.SelectedIndex));
            unitDict.Add("LevelAdd", new PListInteger((long)numericUpDownLv.Value));
            unitDict.Add("TeamLimit", new PListInteger(comboBoxTeamLimit.SelectedIndex));
            unitDict.Add("IsHidden", new PListBool(checkBoxIsHidden.Checked));
            unitDict.Add("IsEnemyHelper", new PListBool(checkBoxIsEnemyHelper.Checked));
            PListDict aiDict = new PListDict();
            unitDict.Add("AIType", aiDict);
            aiDict.Add("Type", new PListInteger(comboBoxAIType.SelectedIndex));
            PListDict targetDict = new PListDict();
            aiDict.Add("Target", targetDict);
            targetDict.Add("CoordX", new PListInteger(System.Int32.Parse(textBoxTargetX.Text)));
            targetDict.Add("CoordY", new PListInteger(System.Int32.Parse(textBoxTargetY.Text)));
            Hero targetHero = UCDrama.heros.Find((h) => { return h.name == comboBoxTargetHero.SelectedItem.ToString(); });
            targetDict.Add("UnitID", new PListInteger(targetHero.num));


            listBoxHeros.Items.Add(hero.name);
            listBoxHeros.SelectedItem = hero.name;
            

        }
       
        private void buttonDel_Click(object sender, EventArgs e)
        {
            string itemDel = listBoxHeros.SelectedItem.ToString();
            listBoxHeros.SelectedIndex = 0;
            Hero hero = UCDrama.heros.Find((h)=>{return h.name==itemDel;});
            curDict.Remove("Unit" + hero.num);
            listBoxHeros.Items.Remove(itemDel);
            if (listBoxHeros.Items.Count <= 0)
                buttonDel.Enabled = false;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (listBoxHeros.SelectedItem == null)
            {
                return;
            }
            Hero originHero = UCDrama.heros.Find((h) => { return h.name == listBoxHeros.SelectedItem.ToString(); });
            Hero hero = UCDrama.heros.Find((h) => { return h.name == comboBoxHero.Text; });
            
            curDict.Remove("Unit" + originHero.num);
            PListDict unitDict = new PListDict();
            curDict.Add("Unit" + hero.num, unitDict);
            unitDict.Add("UnitNum", new PListInteger(hero.num));
            unitDict.Add("CoordX", new PListInteger(System.Int32.Parse(textBoxX.Text)));
            unitDict.Add("CoordY", new PListInteger(System.Int32.Parse(textBoxY.Text)));
            unitDict.Add("Direction", new PListInteger(comboBoxDirection.SelectedIndex));
            unitDict.Add("LevelAdd", new PListInteger((long)numericUpDownLv.Value));
            unitDict.Add("TeamLimit", new PListInteger(comboBoxTeamLimit.SelectedIndex));
            unitDict.Add("IsHidden", new PListBool(checkBoxIsHidden.Checked));
            unitDict.Add("IsEnemyHelper", new PListBool(checkBoxIsEnemyHelper.Checked));
            PListDict aiDict = new PListDict();
            unitDict.Add("AIType", aiDict);
            aiDict.Add("Type", new PListInteger(comboBoxAIType.SelectedIndex));
            PListDict targetDict = new PListDict();
            aiDict.Add("Target", targetDict);
            targetDict.Add("CoordX", new PListInteger(System.Int32.Parse(textBoxTargetX.Text)));
            targetDict.Add("CoordY", new PListInteger(System.Int32.Parse(textBoxTargetY.Text)));
            Hero targetHero = UCDrama.heros.Find((h) => { return h.name == comboBoxTargetHero.SelectedItem.ToString(); });
            targetDict.Add("UnitID", new PListInteger(targetHero.num));

            listBoxHeros.SelectedItem = null;
            listBoxHeros.Items.Remove(originHero.name);
            listBoxHeros.Items.Add(hero.name);
            listBoxHeros.SelectedItem = hero.name;
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            buttonSave_Click(sender, e);
            Hide();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {

        }

        private void comboBoxAIType_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            textBoxTargetX.Visible = false;
            textBoxTargetY.Visible = false;
            comboBoxTargetHero.Visible = false;
            if (comboBoxAIType.SelectedIndex == (int)kAIType.kAITypeAttackUnit
                || comboBoxAIType.SelectedIndex == (int)kAIType.kAITypeFollowUnit)
            {
                comboBoxTargetHero.Visible = true;
            }
            else if (comboBoxAIType.SelectedIndex > 2)
            {
                textBoxTargetX.Visible = true;
                textBoxTargetY.Visible = true;
            }
        }
        


    }
}
